package com.cyz.admin.controller;

import com.cyz.admin.service.UsersService;
import com.cyz.common.dto.UserDto;
import com.cyz.common.utils.R;
import com.cyz.common.validated.AddGroup;
import com.cyz.common.validated.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "管理端用户管理类接口")
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService adminUsersService;

    @PreAuthorize("@ps.hasPermission('system:users:list')")
    @ApiOperation(value = "查询用户信息")
    @GetMapping("/queryUsers")
    public R queryUsers(){
        return adminUsersService.queryUsers();
    }

    @PreAuthorize("@ps.hasPermission('system:users:list')")
    @PutMapping("/updateUser")
    @ApiOperation(value = "更新用户信息")
    public R updateUser(@Validated(UpdateGroup.class) @RequestBody UserDto userDto){
        return adminUsersService.updateUsers(userDto);
    }

    @PreAuthorize("@ps.hasPermission('system:users:list')")
    @PutMapping("/updateUserPassword")
    @ApiOperation(value = "初始化用户密码")
    public R updateUserPassword(Integer id){
        return adminUsersService.updateUserPassword(id);
    }

    @PreAuthorize("@ps.hasPermission('system:users:list')")
    @PutMapping("/updateUserStatus")
    @ApiOperation(value = "更新用户状态")
    public R updateUserStatus(@Validated(UpdateGroup.class) @RequestBody UserDto userDto){
        return adminUsersService.updateUserStatus(userDto);
    }

    @PreAuthorize("@ps.hasPermission('system:users:list')")
    @PostMapping("/addUser")
    @ApiOperation(value = "添加用户")
    public R addUser(@Validated(AddGroup.class) @RequestBody UserDto userDto){
        return adminUsersService.addUser(userDto);
    }

    @PreAuthorize("@ps.hasPermission('system:users:list')")
    @DeleteMapping("/deleteUsers")
    @ApiOperation(value = "删除用户")
    public R deleteUsers(Integer[] ids){
        return adminUsersService.deleteUsers(ids);
    }
}
